package Studentms;

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class Studentms extends WindowAdapter implements ActionListener{
	Frame f;
	Button tf1,tf2;
	Studentms()
	{
		f=new Frame("登陆窗口");
		f.setBounds(500, 500, 400,150);
		tf1=new Button("学生登录");
		tf2=new Button("教师登录");
		tf1.addActionListener(this);
		tf2.addActionListener(this);
 f.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});
		Panel p3=new Panel();
		Panel p4=new Panel();
		p3.add(tf1);
		p4.add(tf2);
		f.add(p3,BorderLayout.NORTH);
		f.add(p4,BorderLayout.SOUTH);
		f.setVisible(true);
	}
	public void actionPerformed(ActionEvent e)
	{
		String cmd =e.getActionCommand();
		if(cmd.equals("学生登录")){
			new RegistSystem1(f);
		}
		else if(cmd.equals("教师登录")){
			new RegistSystem2(f);
		}
	}


public static void main(String[] args) {
		new Studentms();
	}
	class RegistSystem1 extends WindowAdapter implements  ActionListener{
		Frame f1;
		Label l1,l2;
		Button b1;
		Button b2;
		TextField t1; 
		JPasswordField pwdPwd ;
		RegistSystem1(Frame f1) {
			pwdPwd= new JPasswordField(10);
			f1=new Frame("Student");
			f1.setBounds(500, 500, 400,150);
			l1=new Label("学生学号");
			l2=new Label("密码");
			 t1=new TextField(10);
			 b1=new Button("确定");
			 b2=new Button("退出");
			b1.addActionListener(this);
			b2.addActionListener(this);
			f1.addWindowListener(new WindowAdapter()
			{
				public void windowClosing(WindowEvent e)
				{
					System.exit(0);
				}
			});
			Panel p1=new Panel();
			Panel p2=new Panel();
			Panel p3=new Panel();
			p1.add(l1);
			p1.add(t1);
			p2.add(l2);
			p2.add(pwdPwd);
			p3.add(b1);
			p3.add(b2);
			f1.add(p1,BorderLayout.NORTH);
			f1.add(p2,BorderLayout.CENTER);
			f1.add(p3,BorderLayout.SOUTH);
			f1.setVisible(true);
		}	
		public void actionPerformed(ActionEvent e) { 
			String cmd =e.getActionCommand();
			if(cmd.equals("退出")){
				  System.exit(0);
			}
			 String number = t1.getText();
			 String pwd = String.valueOf(pwdPwd.getText());
			 int number1;
			 int pwd1;
			 if(number.equals("")){
				   JOptionPane.showMessageDialog(null,"账号不能为空","错误",JOptionPane.ERROR_MESSAGE);
				   return;
				  }else if (pwd.equals("")){				                  
				   JOptionPane.showMessageDialog(null,"密码不能为空","错误",JOptionPane.ERROR_MESSAGE);			   			    
				   return;
				  }
				  else if((Integer.parseInt(t1.getText())!=-1)&&((Integer.parseInt(pwd)!=-1))){
					  number1=Integer.parseInt(t1.getText());//将学号和密码转换为整形的数，并赋给number1,pwd1
					  pwd1=Integer.parseInt(pwd);}
				 Statement stmt = null;
				 ResultSet rs = null;
				 try {
					 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//注意是JdbcOdbcDriver,而不是JdbcDriver!!!!!!!
					 Connection con=
						 DriverManager.getConnection("jdbc:odbc:SMS","Test","123");//加载驱动器
					 stmt = con.createStatement();                        ////创建STATEMENT对象
					 int i=0;
					 int j=0;
					 int a[][]=new int[10][10];
					 int b[][]=new int[10][10];
		             //int c[][]=new int[10][10];
		             //int d[][]=new int[10][10];
					 rs = stmt.executeQuery("select * from zhanghao");
					 number1=Integer.parseInt(t1.getText());
					 pwd1=Integer.parseInt(pwd); //从表中读数据
					 while (rs.next()) {                                      //rs.next()读的是一行的数据
						 a[i][j]=rs.getInt("ID");                    //用二维数组来存放读出来的数据，
						 b[i][j]=rs.getInt("密码");
		                 //c[i][j]=rs.getInt("教师号");   
		                 //d[i][j]=rs.getInt("密码为");
						 
						 if((number1==a[i][j]&&(pwd1)==b[i][j]))//||(number1==c[i][j]&&(pwd1)==d[i][j])
						 { 
							 new RegistSystem3(f1);//进入到学生信息界面            	  
							 break;
						 } 
					 };
					 if((pwd1)!=b[i][j]&&(pwd1)!=a[i][j])
					 { 
						 JOptionPane.showMessageDialog(null,"学号或密码错误","错误",JOptionPane.ERROR_MESSAGE);
					 } 
					 stmt.close();
					 con.close();
				 } 
				 catch (ClassNotFoundException e1) 
				 {e1.printStackTrace();} 
				 catch (SQLException e1) 
				 { e1.printStackTrace();}
		  }
		}
	}
	class RegistSystem2 implements ActionListener{
		Frame f2;
		Label l1,l2;Button b1;Button b2;
		TextField t1; JPasswordField pwdPwd ;
		RegistSystem2(Frame f2) {
			pwdPwd= new JPasswordField(10);
			f2=new Frame("Teacher");
			f2.setBounds(500, 500, 400,150);
			l1=new Label("教师帐号");
			l2=new Label("密码");
			 t1=new TextField(10);
			 b1=new Button("确定");
			 b2=new Button("退出");
			b1.addActionListener(this);
			b2.addActionListener(this);
			f2.addWindowListener(new WindowAdapter()
			{
				public void windowClosing(WindowEvent e)
				{
					System.exit(0);
				}
			});
			Panel p1=new Panel();
			Panel p2=new Panel();
			Panel p3=new Panel();
			p1.add(l1);
			p1.add(t1);
			p2.add(l2);
			p2.add(pwdPwd);
			p3.add(b1);
			p3.add(b2);
			f2.add(p1,BorderLayout.NORTH);
			f2.add(p2,BorderLayout.CENTER);
			f2.add(p3,BorderLayout.SOUTH);
			f2.setVisible(true);
		}
		public void actionPerformed(ActionEvent e) { 
			String cmd =e.getActionCommand();
			if(cmd.equals("退出")){
				  System.exit(0);
			}
			 String number = t1.getText();
			 String pwd = String.valueOf(pwdPwd.getText());
			 int number1;
			 int pwd1;
			 if(number.equals("")){
				   JOptionPane.showMessageDialog(null,"账号不能为空","错误",JOptionPane.ERROR_MESSAGE);
				   return;
				  }else if (pwd.equals("")){
					                   //把String 类型转换为整形
				   JOptionPane.showMessageDialog(null,"密码不能为空","错误",JOptionPane.ERROR_MESSAGE);
				   return;
				  }
				  else if((Integer.parseInt(t1.getText())!=-1)&&((Integer.parseInt(pwd)!=-1))){
					  number1=Integer.parseInt(t1.getText());//将学号和密码转换为整形的数，并赋给number1,pwd1
					  pwd1=Integer.parseInt(pwd);}

				 Statement stmt = null;
				 ResultSet rs = null;
				 try {
					 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//注意是JdbcOdbcDriver,而不是JdbcDriver!!!!!!!
					 Connection con=
						 DriverManager.getConnection("jdbc:odbc:SMS","Test","123");//加载驱动器
					 stmt = con.createStatement();                        ////创建STATEMENT对象
					 int i=0;
					 int j=0;
					 int a[][]=new int[10][10];
					 int b[][]=new int[10][10];
					 rs = stmt.executeQuery("select * from Teacher");
					 number1=Integer.parseInt(t1.getText());
					 pwd1=Integer.parseInt(pwd); //从表中读数据
					 while (rs.next()) {                                      //rs.next()读的是一行的数据
						 a[i][j]=rs.getInt("ID");                    //用二维数组来存放读出来的数据，
						 b[i][j]=rs.getInt("密码");
						 if((number1==a[i][j]&&(pwd1)==b[i][j]))//||(number1==c[i][j]&&(pwd1)==d[i][j])
						 { 
							 new RegistSystem4();//进入到学生信息界面
							 break;
						 } 
					 };
					 if((pwd1)!=b[i][j]&&(pwd1)!=a[i][j])
					 { 
						 JOptionPane.showMessageDialog(null,"教师账号或密码错误","错误",JOptionPane.ERROR_MESSAGE);
					 } 
					 stmt.close();
					 con.close();
				 } 
				 catch (ClassNotFoundException e1) 
				 {e1.printStackTrace();} 
				 catch (SQLException e1) 
				 { e1.printStackTrace();}
		  }
	}
	class RegistSystem4 extends WindowAdapter implements  ActionListener{
			JFrame f3;
			JMenuBar mb;JMenu 选项;JMenu 退出;JMenuItem 删除;JMenuItem 查询;JMenuItem 修改;JMenuItem 插入;
			
			RegistSystem4() {
				f3=new JFrame("Teacher 已成功登陆");
				f3.setBounds(500, 500, 400,150);
				f3.addWindowListener(new WindowAdapter()
				{
					public void windowClosing(WindowEvent e)
					{
						System.exit(0);
					}
				});
				  mb=new JMenuBar();       //添加菜单,共2个	 
				  选项=new JMenu("选项");退出=new JMenu("退出");
				  f3.setJMenuBar(mb);
				  mb.add(选项);  mb.add(退出);
				删除=new JMenuItem("删除"); 查询=new JMenuItem("查询"); 修改=new JMenuItem("修改"); 插入=new JMenuItem("插入"); 
				选项.add(查询);
				选项.addSeparator(); 
				选项.add(删除);
				选项.addSeparator();     //添加分割符
				选项.add(修改);
				选项.addSeparator();
				选项.add(插入);
				删除.addActionListener( this);
				修改.addActionListener(this);
				插入.addActionListener(this);
				查询.addActionListener(this);
				退出.addActionListener(this);
				f3.setVisible(true);
			}
			private MenuItem MenuItem() {
				return null;
			}
			public void actionPerformed(ActionEvent e) {
				String cmd =e.getActionCommand();
				if(cmd.equals("查询"))
				{
		    //链接数据库，从数据库中输出信息			
				Statement stmt = null;
		        ResultSet rs = null;
		        JFrame jf = new JFrame("学生信息查询系统");
		        JPanel jpl = new JPanel();
		        final String[] colHeads = {"ID", "NAME", "DEPARTMENT", "C++","ENG","GAOSHU","ZUYUAN"};
		        Object[][] base = new Object[11][7];              //创建对象数组 
		        try {
		        	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//注意是JdbcOdbcDriver,而不是JdbcDriver!!!!!!!
		        	Connection con=
					DriverManager.getConnection("jdbc:odbc:SMS","Test","123");//加载驱动器
		            stmt = con.createStatement();                        ////创建STATEMENT对象
		            int i = 0;
		            rs = stmt.executeQuery("select * from STUDENT");       //从表中读数据
		            while (rs.next()) {                                    //rs.next()读的是一行的数据
		                base[i][0] = rs.getInt("ID");                    //用二维数组来存放读出来的数据，
		                base[i][1] = rs.getString("NAME");
		                base[i][2] = rs.getString("DEPARTMENT");
		                base[i][3] = rs.getString("C++");
		                base[i][4] = rs.getString("ENG");
		                base[i][5] = rs.getString("GAOSHU");
		                base[i][6] = rs.getString("ZUYUAN");
		                i++;
		            };//注意有一个分号!!!!!!
		            stmt.close();
				    con.close();                           //关闭与数据库的连接
		        } catch (ClassNotFoundException e1) {
		            e1.printStackTrace();
		        } catch (SQLException e1) {
		            e1.printStackTrace();
		        }
		        JTable table = new JTable(base, colHeads);//创建一个表格，用刚创建的数组做实参
		        table.setPreferredScrollableViewportSize(new Dimension(600, 100));//设置表格的大小
		        JScrollPane pane3 = new JScrollPane(table);       //创建滚动条
		        jpl.setPreferredSize(new Dimension(600, 400));
		        jpl.add(pane3);                        //把滚动条添加到容器上
		        JLabel pitLabel1 = new JLabel(); //new ImageIcon("2.JPEG")  //用小图片装饰组建
		        pitLabel1.setBounds(0, -10, 700, 500);                              //设置标签大小
		        jpl.add(pitLabel1);                                                //把标签条添加到容器上
		        jf.setContentPane(jpl);                                           //把容器添加到窗体上
		        jf.pack();
		        jf.show();
			}
				else if(cmd.equals("退出"))
				{ 
					System.exit(0);
					}
				else if(cmd.equals("插入")){
					return;
				}
				else if(cmd.equals("删除")){
					JFrame f4;
					f4=new JFrame("请填写要删出学生的ID");
					f4.setBounds(500, 500, 400,150);
					f4.addWindowListener(new WindowAdapter()
					{
						public void windowClosing(WindowEvent e)
						{
							System.exit(0);
						}
					});
					JLabel labName=new JLabel("ID：");
				    JTextField txtID=new JTextField(10);
				    JButton btnDel=new JButton("确定");
				    JButton btnCancel=new JButton("取消");
				    JPanel pan1=new JPanel();
				    JPanel pan2=new JPanel();
				    pan1.add(labName);
				    pan1.add(txtID);
				    pan2.add(btnDel);
				    pan2.add(btnCancel);
				    f4.add(pan1,BorderLayout.NORTH);
					f4.add(pan2,BorderLayout.SOUTH);
					f4.setVisible(true);
					btnDel.addActionListener(this);
					btnCancel.addActionListener(this);
					String cm =e.getActionCommand();
				    Connection cnn=null;
				    int ID;
				    int number;
				    String num = txtID.getText();
				    final String[] colHeads = {"ID", "NAME", "DEPARTMENT", "C++","ENG","GAOSHU","ZUYUAN"};
			        Object[][] base = new Object[11][7];  
			        btnDel.setEnabled(true);
			        if(num.equals("")){
						  JOptionPane.showMessageDialog(null,"ID不能为空","错误",JOptionPane.ERROR_MESSAGE);
						  return;
						  }
					else if((Integer.parseInt(txtID.getText())!=-1)){
						ID=Integer.parseInt(txtID.getText());//将学号和密码转换为整形的数，并赋给number1,pwd1
					}

			        Statement stm= null;
					ResultSet rs = null;
					try {
							 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//注意是JdbcOdbcDriver,而不是JdbcDriver!!!!!!!
							 Connection con=
								 DriverManager.getConnection("jdbc:odbc:SMS","Test","123");//加载驱动器
							 stm = con.createStatement();  ////创建STATEMENT对象
							 rs = stm.executeQuery("select * from STUDENT");
							 number=rs.getInt("ID");
							 stm.executeUpdate("DELETE FROM  STUDENT WHERE ID='txtID.getText()'");
							 btnDel.setEnabled(true);
							 txtID.setText("");
							 int i=0;
							 int j=0;
							 int a[][]=new int[10][10];
							 rs = stm.executeQuery("select * from STUDENT");
							 ID=Integer.parseInt(txtID.getText());
							 while (rs.next()) {                                      //rs.next()读的是一行的数据
								 base[i][0] = rs.getInt("ID");                    //用二维数组来存放读出来的数据，
									base[i][1] = rs.getString("NAME");
									base[i][2] = rs.getString("DEPARTMENT");
									base[i][3] = rs.getString("C++");
									base[i][4] = rs.getString("ENG");
									base[i][5] = rs.getString("GAOSHU");
									base[i][6] = rs.getString("ZUYUAN");//用二维数组来存放读出来的数据，
							 };
							
							 if(ID!=a[i][j])
							 { 
								 JOptionPane.showMessageDialog(null,"学生不存在","错误",JOptionPane.ERROR_MESSAGE);
							 } 
				          //注意有一个分号!!!!!!
							 stm.close();
							 con.close();
						 } 
						 catch (ClassNotFoundException e1) 
						 {e1.printStackTrace();} 
						 catch (SQLException e1) 
						 { e1.printStackTrace();}
				  }
				}
			}
	class RegistSystem3 extends WindowAdapter implements  ActionListener{
		Frame f3;
		Button b1;
		Button b2;
		Button b3;
		RegistSystem3(Frame f3) {
			f3=new Frame();
			f3=new Frame("Student 已成功登陆");
			f3.setBounds(500, 500, 400,150);
			b1=new Button("查询");
			b2=new Button("退出");
			b3=new Button("返回");
			b1.addActionListener(this);
			b2.addActionListener(this);
			b3.addActionListener(this);
			f3.addWindowListener(new WindowAdapter()
			{
				public void windowClosing(WindowEvent e)
				{
					System.exit(0);
				}
			});
			Panel p1=new Panel();
			Panel p2=new Panel();
			Panel p3=new Panel();
			p1.add(b1);
			p2.add(b2);
			p3.add(b3);
			f3.add(p1,BorderLayout.NORTH);
			f3.add(p3,BorderLayout.CENTER);
			f3.add(p2,BorderLayout.SOUTH);
			f3.setVisible(true);
		}
		public void actionPerformed(ActionEvent e) {
			String cmd =e.getActionCommand();
			if(cmd.equals("查询")){
				Statement stmt = null;
		        ResultSet rs = null;
		        JFrame jf = new JFrame("学生信息查询系统");
		        JPanel jpl = new JPanel();
		        final String[] colHeads = {"ID", "NAME", "DEPARTMENT", "C++","ENG","GAOSHU","ZUYUAN"};
		        Object[][] base = new Object[11][7];              //创建对象数组 
		        try {
		        	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//注意是JdbcOdbcDriver,而不是JdbcDriver!!!!!!!
		        	Connection con=
					DriverManager.getConnection("jdbc:odbc:SMS","Test","123");//加载驱动器
		            stmt = con.createStatement();                        ////创建STATEMENT对象
		            int i = 0;
		            rs = stmt.executeQuery("select * from STUDENT");       //从表中读数据
		            while (rs.next()) {                                    //rs.next()读的是一行的数据
		                base[i][0] = rs.getInt("ID");                    //用二维数组来存放读出来的数据，
		                base[i][1] = rs.getString("NAME");
		                base[i][2] = rs.getString("DEPARTMENT");
		                base[i][3] = rs.getString("C++");
		                base[i][4] = rs.getString("ENG");
		                base[i][5] = rs.getString("GAOSHU");
		                base[i][6] = rs.getString("ZUYUAN");
		                i++;
		            };//注意有一个分号!!!!!!
		            stmt.close();
				    con.close();                           //关闭与数据库的连接

		        } catch (ClassNotFoundException e1) {
		            e1.printStackTrace();
		        } catch (SQLException e1) {
		            e1.printStackTrace();
		        }
		       //建立一个二维表
		        JTable table = new JTable(base, colHeads);//创建一个表格，用刚创建的数组做实参
		        table.setPreferredScrollableViewportSize(new Dimension(600, 100));//设置表格的大小
		        JScrollPane pane3 = new JScrollPane(table);       //创建滚动条
		        jpl.setPreferredSize(new Dimension(600, 400));
		        jpl.add(pane3);                        //把滚动条添加到容器上
		        JLabel pitLabel1 = new JLabel(); //new ImageIcon("2.JPEG")  //用小图片装饰组建
		        pitLabel1.setBounds(0, -10, 700, 500);                              //设置标签大小
		        jpl.add(pitLabel1);                                                //把标签条添加到容器上
		        jf.setContentPane(jpl);                                           //把容器添加到窗体上
		        jf.pack();
		        jf.show();
		  
			}
			else if(cmd.equals("返回")){
				System.exit(0);
			}
			else if(cmd.equals("退出")){
				System.exit(0);
			}
			
		}
}

